package com.chap15;

import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.util.concurrent.Executors;

/**
 * <p>
 * <code>DisruptorDemo</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-18 14:58
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class DisruptorDemo4 {
    public static void main(String[] args) {
        Disruptor<OrderEvent> orderEventDisruptor = new Disruptor<>(new OrderEventFactory(),
                1024 * 1024,
                Executors.defaultThreadFactory(),
                ProducerType.MULTI, new YieldingWaitStrategy());
        //消费者优先级
        //消费者A先消费  BC才可以消费  D才可以消费
        //一个消息 A 消费了 B C有一个可以消费 D可以消费
        orderEventDisruptor.handleEventsWith(new OrderEventHandler())
                .thenHandleEventsWithWorkerPool(new OrderEventHandler(),new OrderEventHandler())
                .then(new OrderEventHandler());
        orderEventDisruptor.start();

        RingBuffer<OrderEvent> ringBuffer = orderEventDisruptor.getRingBuffer();
        new Thread(()->{
            OrderEventProducer orderEventProducer = new OrderEventProducer(ringBuffer);
            for (int i = 0; i < 100; i++) {
                orderEventProducer.onData(i,"zhangsan"+i);

            }

        }).start();
        new Thread(()->{
            OrderEventProducer orderEventProducer = new OrderEventProducer(ringBuffer);
            for (int i = 0; i < 100; i++) {
                orderEventProducer.onData(i,"lisi"+i);

            }

        }).start();
//        orderEventDisruptor.shutdown();
    }
}
